package com.hxf.web.action;

import java.io.IOException;
import java.util.List;

import com.hxf.common.Node;
import com.hxf.common.PublicUtil;
import com.hxf.common.Tree;
import com.hxf.dto.MenuDto;
import com.hxf.exception.ServiceException;

/**
 * 后台管理Action
 * @author tengyicheng
 * @version [1.0, 2013/09/22]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class MainAction extends WebActionBase {
	private static final long serialVersionUID = 1L;

	public String index() {
		 
		return "index";
	}
	
	public String left() {
		
		return "main-left";
	}
	
	/**
	 * 树形菜单
	 * @return
	 */
	public String tree() { 
		try {
			
			Object adminUserDto = getSession().getAttribute("adminUserDto"); //联盟管理员Session
			Object restaurentUserDto = getSession().getAttribute("restaurentUserDtoLogin");//餐厅 Session 
			
			//联盟管理员
			if(null != adminUserDto){
				getRequest().setCharacterEncoding("UTF-8");
				getResponse().setContentType("text/xml; charset=UTF-8");
				//后台管理员
				List<MenuDto> menuList = super.serviceFacade.getMenuService().findMenuTree(getAdminUser().getId());
				Tree tree = new Tree();
				Node n = null; 
				for (MenuDto menu : menuList) { 
					if (PublicUtil.isNotNullAndEmpty(menu.getMenuUrl())) {
						n = new Node(String.valueOf(menu.getMenuId()), menu.getMenuName(), String.valueOf(menu.getParentId()), menu.getMenuUrl(), "leaf.gif");
					} else {
						n = new Node(String.valueOf(menu.getMenuId()), menu.getMenuName(), String.valueOf(menu.getParentId()), menu.getMenuUrl(),"folderOpen.gif");
					}
					tree.addNode(n);
				} 
				
				println(tree.toXMLString());
			}else if(null != restaurentUserDto){  //餐厅人员
				getRequest().setCharacterEncoding("UTF-8");
				getResponse().setContentType("text/xml; charset=UTF-8");
				List<MenuDto> menuList = super.serviceFacade.getMenuService().RestaurentfindMenuTree(getRestaurentUserDto().getId());
				Tree tree = new Tree();
				Node n = null; 
				for (MenuDto menu : menuList) { 
					if (PublicUtil.isNotNullAndEmpty(menu.getMenuUrl())) {
						n = new Node(String.valueOf(menu.getMenuId()), menu.getMenuName(), String.valueOf(menu.getParentId()), menu.getMenuUrl(), "leaf.gif");
					} else {
						n = new Node(String.valueOf(menu.getMenuId()), menu.getMenuName(), String.valueOf(menu.getParentId()), menu.getMenuUrl(),"folderOpen.gif");
					}
					tree.addNode(n);
				} 
				
				println(tree.toXMLString());
			}
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}

	public String right() {
		return "main-right";
	}
	
	public String top() {
		return "main-top";
	}
	
	public String bottom() {
		return "main-bottom";
	}
}
